00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef GENMATRIXMAP_HPP_
00018 #define GENMATRIXMAP_HPP_
00019
00020 #define NZ_PER_ROW
00021
00022 #include <boost/smart_ptr/shared_ptr.hpp>
00023 #include <ga.h>
00024 #include "gridpack/parallel/parallel.hpp"
00025 #include <gridpack/parallel/distributed.hpp>
00026 #include <gridpack/component/base_component.hpp>
00027 #include <gridpack/network/base_network.hpp>
00028 #include <gridpack/math/matrix.hpp>
00029 #include <gridpack/utilities/exception.hpp>
00030
00031
00032
00033 namespace gridpack {
00034 namespace mapper {
00035
00036 template <class _network>
00037 class GenMatrixMap {
00038 public:
00039
00040
00041
00042
00043
00044
00045 GenMatrixMap(boost::shared_ptr<_network> network)
00046 : p_network(network)
00047 {
00048 p_row_Offsets = NULL;
00049 p_col_Offsets = NULL;
00050 #ifdef NZ_PER_ROW
00051 p_nz_per_row = NULL;
00052 #endif
00053
00054 p_timer = NULL;
00055
00056
00057 p_GAgrp = network->communicator().getGroup();
00058 p_me = GA_Pgroup_nodeid(p_GAgrp);
00059 p_nNodes = GA_Pgroup_nnodes(p_GAgrp);
00060
00061 p_row_Offsets = new int[p_nNodes];
00062 p_col_Offsets = new int[p_nNodes];
00063
00064 p_nBuses = p_network->numBuses();
00065 p_nBranches = p_network->numBranches();
00066
00067 getDimensions();
00068 setOffsets();
00069 setIndices();
00070 numberNonZeros();
00071 GA_Pgroup_sync(p_GAgrp);
00072 }
00073
00074 ~GenMatrixMap()
00075 {
00076 if (p_row_Offsets != NULL) delete [] p_row_Offsets;
00077 if (p_col_Offsets != NULL) delete [] p_col_Offsets;
00078 #ifdef NZ_PER_ROW
00079 if (p_nz_per_row != NULL) delete [] p_nz_per_row;
00080 #endif
00081 GA_Pgroup_sync(p_GAgrp);
00082 }
00083
00084
00085
00086
00087
00088 boost::shared_ptr<gridpack::math::Matrix> mapToMatrix(void)
00089 {
00090 gridpack::parallel::Communicator comm = p_network->communicator();
00091 int blockSize = p_maxRowIndex-p_minRowIndex+1;
00092 boost::shared_ptr<gridpack::math::Matrix>
00093 Ret(new gridpack::math::Matrix(comm, blockSize, p_colBlockSize, p_nz_per_row));
00094 loadBusData(*Ret,false);
00095 loadBranchData(*Ret,false);
00096 GA_Pgroup_sync(p_GAgrp);
00097 Ret->ready();
00098 return Ret;
00099 }
00100
00101
00102
00103
00104
00105
00106 gridpack::math::Matrix* intMapToMatrix(void)
00107 {
00108 gridpack::parallel::Communicator comm = p_network->communicator();
00109 int blockSize = p_maxRowIndex-p_minRowIndex+1;
00110 gridpack::math::Matrix*
00111 Ret(new gridpack::math::Matrix(comm, blockSize, p_colBlockSize, p_nz_per_row));
00112 loadBusData(*Ret,false);
00113 loadBranchData(*Ret,false);
00114 GA_Pgroup_sync(p_GAgrp);
00115 Ret->ready();
00116 return Ret;
00117 }
00118
00119
00120
00121
00122
00123
00124 void mapToMatrix(gridpack::math::Matrix &matrix)
00125 {
00126 int t_set, t_bus, t_branch;
00127 matrix.zero();
00128 loadBusData(matrix,false);
00129 loadBranchData(matrix,false);
00130 GA_Pgroup_sync(p_GAgrp);
00131 matrix.ready();
00132 }
00133
00134
00135
00136
00137
00138 void mapToMatrix(boost::shared_ptr<gridpack::math::Matrix> &matrix)
00139 {
00140 mapToMatrix(*matrix);
00141 }
00142
00143
00144
00145
00146
00147
00148 void overwriteMatrix(gridpack::math::Matrix &matrix)
00149 {
00150 loadBusData(matrix,false);
00151 loadBranchData(matrix,false);
00152 GA_Pgroup_sync(p_GAgrp);
00153 matrix.ready();
00154 }
00155
00156
00157
00158
00159
00160
00161 void overwriteMatrix(boost::shared_ptr<gridpack::math::Matrix> &matrix)
00162 {
00163 overwriteMatrix(*matrix);
00164 }
00165
00166
00167
00168
00169
00170
00171 void incrementMatrix(gridpack::math::Matrix &matrix)
00172 {
00173 loadBusData(matrix,true);
00174 loadBranchData(matrix,true);
00175 GA_Pgroup_sync(p_GAgrp);
00176 matrix.ready();
00177 }
00178
00179
00180
00181
00182
00183
00184 void incrementMatrix(boost::shared_ptr<gridpack::math::Matrix> &matrix)
00185 {
00186 incrementMatrix(*matrix);
00187 }
00188
00189 private:
00190
00191
00192
00193
00194
00195 bool isLocalBranch(int idx)
00196 {
00197 int jdx1, jdx2;
00198 p_network->getBranchEndpoints(idx, &jdx1, &jdx2);
00199 bool check = true;
00200 check = check && p_network->getActiveBus(jdx1);
00201 check = check && p_network->getActiveBus(jdx2);
00202 return check;
00203 }
00204
00205
00206
00207
00208
00209 void getDimensions(void)
00210 {
00211 int i, nval;
00212
00213 int nRows = 0;
00214 int nCols = 0;
00215
00216 for (i=0; i<p_nBuses; i++) {
00217 if (p_network->getActiveBus(i)) {
00218 nval = p_network->getBus(i)->matrixNumRows();
00219 nRows += nval;
00220 nval = p_network->getBus(i)->matrixNumCols();
00221 nCols += nval;
00222 }
00223 }
00224
00225 for (i=0; i<p_nBranches; i++) {
00226 if (p_network->getActiveBranch(i)) {
00227 nval = p_network->getBranch(i)->matrixNumRows();
00228 nRows += nval;
00229 nval = p_network->getBranch(i)->matrixNumCols();
00230 nCols += nval;
00231 }
00232 }
00233 p_colBlockSize = nCols;
00234
00235 int *sizebuf = new int[2*p_nNodes];
00236 for (i=0; i<2*p_nNodes; i++) {
00237 sizebuf[i] = 0;
00238 }
00239 sizebuf[2*p_me] = nRows;
00240 sizebuf[2*p_me+1] = nCols;
00241 char plus[2];
00242 strcpy(plus,"+");
00243 GA_Pgroup_igop(p_GAgrp, sizebuf, 2*p_nNodes, plus);
00244
00245 p_iDim = sizebuf[0];
00246 p_jDim = sizebuf[1];
00247 p_row_Offsets[0] = 0;
00248 p_col_Offsets[0] = 0;
00249 for (i=1; i<p_nNodes; i++) {
00250 p_iDim += sizebuf[2*i];
00251 p_jDim += sizebuf[2*i+1];
00252 p_row_Offsets[i] = p_row_Offsets[i-1] + sizebuf[2*(i-1)];
00253 p_col_Offsets[i] = p_col_Offsets[i-1] + sizebuf[2*(i-1)+1];
00254 }
00255 delete [] sizebuf;
00256 }
00257
00258
00259
00260
00261 void setOffsets(void)
00262 {
00263
00264
00265 int i,j,jdx,jdx1,jdx2;
00266 int *i_bus_offsets = new int[p_nBuses];
00267 int *i_branch_offsets = new int[p_nBranches];
00268 int *j_bus_offsets = new int[p_nBuses];
00269 int *j_branch_offsets = new int[p_nBranches];
00270 for (i=0; i<p_nBuses; i++) {
00271 i_bus_offsets[i] = 0;
00272 j_bus_offsets[i] = 0;
00273 }
00274 for (i=0; i<p_nBranches; i++) {
00275 i_branch_offsets[i] = 0;
00276 j_branch_offsets[i] = 0;
00277 }
00278 int icnt = 0;
00279 int jcnt = 0;
00280 int nsize;
00281
00282 for (i=0; i<p_nBuses; i++) {
00283 if (p_network->getActiveBus(i)) {
00284 i_bus_offsets[i] = icnt;
00285 icnt += p_network->getBus(i)->matrixNumRows();
00286 j_bus_offsets[i] = jcnt;
00287 jcnt += p_network->getBus(i)->matrixNumCols();
00288 std::vector<int> nghbrs = p_network->getConnectedBranches(i);
00289 nsize = nghbrs.size();
00290 for (j=0; j<nsize; j++) {
00291
00292
00293
00294
00295 jdx = nghbrs[j];
00296 if (isLocalBranch(jdx)) {
00297 p_network->getBranchEndpoints(jdx,&jdx1,&jdx2);
00298 if (jdx1 == i) {
00299 i_branch_offsets[jdx] = icnt;
00300 icnt += p_network->getBranch(jdx)->matrixNumRows();
00301 j_branch_offsets[jdx] = jcnt;
00302 jcnt += p_network->getBranch(jdx)->matrixNumCols();
00303 }
00304 } else {
00305 if (p_network->getActiveBranch(jdx)) {
00306 i_branch_offsets[jdx] = icnt;
00307 icnt += p_network->getBranch(jdx)->matrixNumRows();
00308 j_branch_offsets[jdx] = jcnt;
00309 jcnt += p_network->getBranch(jdx)->matrixNumCols();
00310 }
00311 }
00312 }
00313 }
00314 }
00315
00316
00317 int **i_bus_index = new int*[p_nBuses];
00318 int **j_bus_index = new int*[p_nBuses];
00319 int **i_branch_index = new int*[p_nBranches];
00320 int **j_branch_index = new int*[p_nBranches];
00321 int *i_bus_index_buf = new int[p_nBuses];
00322 int *j_bus_index_buf = new int[p_nBuses];
00323 int *i_branch_index_buf = new int[p_nBranches];
00324 int *j_branch_index_buf = new int[p_nBranches];
00325 int *i_bus_value_buf = new int[p_nBuses];
00326 int *j_bus_value_buf = new int[p_nBuses];
00327 int *i_branch_value_buf = new int[p_nBranches];
00328 int *j_branch_value_buf = new int[p_nBranches];
00329 int i_bus_cnt = 0;
00330 int j_bus_cnt = 0;
00331 int i_branch_cnt = 0;
00332 int j_branch_cnt = 0;
00333 int row_offset = p_row_Offsets[p_me];
00334 int col_offset = p_col_Offsets[p_me];
00335 int nbus = 0;
00336 int nbranch = 0;
00337 for (i=0; i<p_nBuses; i++) {
00338 if (p_network->getActiveBus(i)) {
00339 nbus++;
00340 i_bus_value_buf[i_bus_cnt] = i_bus_offsets[i]+row_offset;
00341 i_bus_index_buf[i_bus_cnt] = p_network->getGlobalBusIndex(i);
00342 i_bus_index[i_bus_cnt] = &i_bus_index_buf[i_bus_cnt];
00343 i_bus_cnt++;
00344
00345 j_bus_value_buf[j_bus_cnt] = j_bus_offsets[i]+col_offset;
00346 j_bus_index_buf[j_bus_cnt] = p_network->getGlobalBusIndex(i);
00347 j_bus_index[j_bus_cnt] = &j_bus_index_buf[j_bus_cnt];
00348 j_bus_cnt++;
00349 }
00350 }
00351 for (i=0; i<p_nBranches; i++) {
00352 if (p_network->getActiveBranch(i)) {
00353 nbranch++;
00354 i_branch_value_buf[i_branch_cnt] = i_branch_offsets[i]+row_offset;
00355 i_branch_index_buf[i_branch_cnt] = p_network->getGlobalBranchIndex(i);
00356 i_branch_index[i_branch_cnt] = &i_branch_index_buf[i_branch_cnt];
00357 i_branch_cnt++;
00358
00359 j_branch_value_buf[j_branch_cnt] = j_branch_offsets[i]+col_offset;
00360 j_branch_index_buf[j_branch_cnt] = p_network->getGlobalBranchIndex(i);
00361 j_branch_index[j_branch_cnt] = &j_branch_index_buf[j_branch_cnt];
00362 j_branch_cnt++;
00363 }
00364 }
00365 delete [] i_bus_offsets;
00366 delete [] j_bus_offsets;
00367 delete [] i_branch_offsets;
00368 delete [] j_branch_offsets;
00369
00370
00371 int *t_busMap = new int[p_nNodes];
00372 int *t_branchMap = new int[p_nNodes];
00373 for (i=0; i<p_nNodes; i++) {
00374 t_busMap[i] = 0;
00375 t_branchMap[i] = 0;
00376 }
00377 t_busMap[p_me] = nbus;
00378 t_branchMap[p_me] = nbranch;
00379 char plus[2];
00380 strcpy(plus,"+");
00381 GA_Pgroup_igop(p_GAgrp, t_busMap, p_nNodes, plus);
00382 GA_Pgroup_igop(p_GAgrp, t_branchMap, p_nNodes, plus);
00383 int *busMap = new int[p_nNodes];
00384 int *branchMap = new int[p_nNodes];
00385 busMap[0] = 0;
00386 branchMap[0] = 0;
00387 int total_buses = t_busMap[0];
00388 int total_branches = t_branchMap[0];
00389 for (i=1; i<p_nNodes; i++) {
00390 busMap[i] = busMap[i-1] + t_busMap[i-1];
00391 total_buses += t_busMap[i];
00392 branchMap[i] = branchMap[i-1] + t_branchMap[i-1];
00393 total_branches += t_branchMap[i];
00394 }
00395 delete [] t_busMap;
00396 delete [] t_branchMap;
00397
00398 int one = 1;
00399 g_bus_row_offsets = GA_Create_handle();
00400 GA_Set_data(g_bus_row_offsets, one, &total_buses, C_INT);
00401 GA_Set_irreg_distr(g_bus_row_offsets, busMap, &p_nNodes);
00402 GA_Set_pgroup(g_bus_row_offsets, p_GAgrp);
00403 if (!GA_Allocate(g_bus_row_offsets)) {
00404 char buf[256];
00405 sprintf(buf,"GenMatrixMap::setOffsets: Unable to allocate distributed array for bus row offsets\n");
00406 printf("%s",buf);
00407 throw gridpack::Exception(buf);
00408 }
00409 GA_Zero(g_bus_row_offsets);
00410
00411 g_bus_column_offsets = GA_Create_handle();
00412 GA_Set_data(g_bus_column_offsets, one, &total_buses, C_INT);
00413 GA_Set_irreg_distr(g_bus_column_offsets, busMap, &p_nNodes);
00414 GA_Set_pgroup(g_bus_column_offsets, p_GAgrp);
00415 if (!GA_Allocate(g_bus_column_offsets)) {
00416 char buf[256];
00417 sprintf(buf,"GenMatrixMap::setOffsets: Unable to allocate distributed array for bus column offsets\n");
00418 printf("%s",buf);
00419 throw gridpack::Exception(buf);
00420 }
00421 GA_Zero(g_bus_column_offsets);
00422
00423 g_branch_row_offsets = GA_Create_handle();
00424 GA_Set_data(g_branch_row_offsets, one, &total_branches, C_INT);
00425 GA_Set_irreg_distr(g_branch_row_offsets, branchMap, &p_nNodes);
00426 GA_Set_pgroup(g_branch_row_offsets, p_GAgrp);
00427 if (!GA_Allocate(g_branch_row_offsets)) {
00428 char buf[256];
00429 sprintf(buf,"GenMatrixMap::setOffsets: Unable to allocate distributed array for branch row offsets\n");
00430 printf("%s",buf);
00431 throw gridpack::Exception(buf);
00432 }
00433 GA_Zero(g_branch_row_offsets);
00434
00435 g_branch_column_offsets = GA_Create_handle();
00436 GA_Set_data(g_branch_column_offsets, one, &total_branches, C_INT);
00437 GA_Set_irreg_distr(g_branch_column_offsets, branchMap, &p_nNodes);
00438 GA_Set_pgroup(g_branch_column_offsets, p_GAgrp);
00439 if (!GA_Allocate(g_branch_column_offsets)) {
00440 char buf[256];
00441 sprintf(buf,"GenMatrixMap::setOffsets: Unable to allocate distributed array for branch column offsets\n");
00442 printf("%s",buf);
00443 throw gridpack::Exception(buf);
00444 }
00445 GA_Zero(g_branch_column_offsets);
00446
00447 delete [] busMap;
00448 delete [] branchMap;
00449
00450
00451 NGA_Scatter(g_bus_row_offsets, i_bus_value_buf, i_bus_index, i_bus_cnt);
00452 NGA_Scatter(g_bus_column_offsets, j_bus_value_buf, j_bus_index, j_bus_cnt);
00453 NGA_Scatter(g_branch_row_offsets, i_branch_value_buf, i_branch_index, i_branch_cnt);
00454 NGA_Scatter(g_branch_column_offsets, j_branch_value_buf, j_branch_index, j_branch_cnt);
00455 GA_Pgroup_sync(p_GAgrp);
00456
00457 delete [] i_bus_index;
00458 delete [] j_bus_index;
00459 delete [] i_branch_index;
00460 delete [] j_branch_index;
00461
00462 delete [] i_bus_index_buf;
00463 delete [] j_bus_index_buf;
00464 delete [] i_branch_index_buf;
00465 delete [] j_branch_index_buf;
00466 delete [] i_bus_value_buf;
00467 delete [] j_bus_value_buf;
00468 delete [] i_branch_value_buf;
00469 delete [] j_branch_value_buf;
00470 }
00471
00472
00473
00474
00475
00476
00477 void setIndices(void)
00478 {
00479
00480 int **bus_index = new int*[p_nBuses];
00481 int **branch_index = new int*[p_nBranches];
00482 int *bus_index_buf = new int[p_nBuses];
00483 int *branch_index_buf = new int[p_nBranches];
00484 int *i_bus_value_buf = new int[p_nBuses];
00485 int *j_bus_value_buf = new int[p_nBuses];
00486 int *i_branch_value_buf = new int[p_nBranches];
00487 int *j_branch_value_buf = new int[p_nBranches];
00488 int i, j;
00489
00490 for (i=0; i<p_nBuses; i++) {
00491 bus_index_buf[i] = p_network->getGlobalBusIndex(i);
00492 bus_index[i] = &bus_index_buf[i];
00493 }
00494 for (i=0; i<p_nBranches; i++) {
00495 branch_index_buf[i] = p_network->getGlobalBranchIndex(i);
00496 branch_index[i] = &branch_index_buf[i];
00497 }
00498 NGA_Gather(g_bus_row_offsets, i_bus_value_buf, bus_index, p_nBuses);
00499 NGA_Gather(g_bus_column_offsets, j_bus_value_buf, bus_index, p_nBuses);
00500 NGA_Gather(g_branch_row_offsets, i_branch_value_buf, branch_index, p_nBranches);
00501 NGA_Gather(g_branch_column_offsets, j_branch_value_buf, branch_index, p_nBranches);
00502
00503
00504 int offset, nrows, ncols, idx;
00505 for (i=0; i<p_nBuses; i++) {
00506 nrows = p_network->getBus(i)->matrixNumRows();
00507 if (nrows > 0) {
00508 offset = i_bus_value_buf[i];
00509 for (j=0; j<nrows; j++) {
00510 idx = offset+j;
00511 p_network->getBus(i)->matrixSetRowIndex(j,idx);
00512 }
00513 }
00514 ncols = p_network->getBus(i)->matrixNumCols();
00515 if (ncols > 0) {
00516 offset = j_bus_value_buf[i];
00517 for (j=0; j<ncols; j++) {
00518 idx = offset+j;
00519 p_network->getBus(i)->matrixSetColIndex(j,idx);
00520 }
00521 }
00522 }
00523 for (i=0; i<p_nBranches; i++) {
00524 nrows = p_network->getBranch(i)->matrixNumRows();
00525 if (nrows > 0) {
00526 offset = i_branch_value_buf[i];
00527 for (j=0; j<nrows; j++) {
00528 idx = offset+j;
00529 p_network->getBranch(i)->matrixSetRowIndex(j,idx);
00530 }
00531 }
00532 ncols = p_network->getBranch(i)->matrixNumCols();
00533 if (ncols > 0) {
00534 offset = j_branch_value_buf[i];
00535 for (j=0; j<ncols; j++) {
00536 idx = offset+j;
00537 p_network->getBranch(i)->matrixSetColIndex(j,idx);
00538 }
00539 }
00540 }
00541
00542 delete [] bus_index;
00543 delete [] branch_index;
00544
00545 delete [] bus_index_buf;
00546 delete [] branch_index_buf;
00547 delete [] i_bus_value_buf;
00548 delete [] j_bus_value_buf;
00549 delete [] i_branch_value_buf;
00550 delete [] j_branch_value_buf;
00551
00552
00553 GA_Destroy(g_bus_row_offsets);
00554 GA_Destroy(g_bus_column_offsets);
00555 GA_Destroy(g_branch_row_offsets);
00556 GA_Destroy(g_branch_column_offsets);
00557 }
00558
00559
00560
00561
00562 void numberNonZeros(void)
00563 {
00564
00565 p_minRowIndex = p_row_Offsets[p_me];
00566 if (p_me < p_nNodes-1) {
00567 p_maxRowIndex = p_row_Offsets[p_me+1] - 1;
00568 } else {
00569 p_maxRowIndex = p_iDim-1;
00570 }
00571 int dim = p_maxRowIndex - p_minRowIndex + 1;
00572 int *row_idx_buf = new int[dim];
00573 p_nz_per_row = new int[dim];
00574 int i, j;
00575 for (i=0; i<dim; i++) {
00576 row_idx_buf[i] = i+p_minRowIndex;
00577 p_nz_per_row[i] = 0;
00578 }
00579 delete [] row_idx_buf;
00580 int nvals;
00581 p_maxValues = 0;
00582 for (i=0; i<p_nBuses; i++) {
00583 if (p_network->getActiveBus(i)) {
00584 nvals = p_network->getBus(i)->matrixNumValues();
00585 if (nvals > p_maxValues) p_maxValues = nvals;
00586 if (nvals > 0) {
00587 gridpack::ComplexType *values = new gridpack::ComplexType[nvals];
00588 int *rows = new int[nvals];
00589 int *cols = new int[nvals];
00590 p_network->getBus(i)->matrixGetValues(values, rows, cols);
00591 for (j=0; j<nvals; j++) {
00592
00593 p_nz_per_row[rows[j]-p_minRowIndex]++;
00594
00595 }
00596 delete [] rows;
00597 delete [] cols;
00598 delete [] values;
00599 }
00600 }
00601 }
00602 for (i=0; i<p_nBranches; i++) {
00603 nvals = p_network->getBranch(i)->matrixNumValues();
00604 if (nvals > p_maxValues) p_maxValues = nvals;
00605 if (nvals > 0) {
00606 int ncols = p_network->getBranch(i)->matrixNumCols();
00607 int rmin, rmax;
00608 bool isActive = p_network->getActiveBranch(i);
00609 if (ncols > 0) {
00610 rmin = p_network->getBranch(i)->matrixGetRowIndex(0);
00611 rmax = p_network->getBranch(i)->matrixGetRowIndex(ncols-1);
00612 }
00613 gridpack::ComplexType *values = new gridpack::ComplexType[nvals];
00614 int *rows = new int[nvals];
00615 int *cols = new int[nvals];
00616 p_network->getBranch(i)->matrixGetValues(values, rows, cols);
00617 for (j=0; j<nvals; j++) {
00618 if (rows[j] >= p_minRowIndex && rows[j] <= p_maxRowIndex) {
00619 if (ncols > 0) {
00620 if (cols[j] >= rmin && cols[j] <= rmax) {
00621 if (isActive) p_nz_per_row[rows[j]-p_minRowIndex]++;
00622 } else {
00623 p_nz_per_row[rows[j]-p_minRowIndex]++;
00624 }
00625 } else {
00626 p_nz_per_row[rows[j]-p_minRowIndex]++;
00627 }
00628 }
00629 }
00630 delete [] rows;
00631 delete [] cols;
00632 delete [] values;
00633 }
00634 }
00635 }
00636
00637
00638
00639
00640
00641
00642 void loadBusData(gridpack::math::Matrix &matrix, bool flag)
00643 {
00644 int i, j, nvals;
00645 ComplexType *values = new ComplexType[p_maxValues];
00646 int *rows = new int[p_maxValues];
00647 int *cols = new int[p_maxValues];
00648 for (i=0; i<p_nBuses; i++) {
00649 if (p_network->getActiveBus(i)) {
00650 nvals = p_network->getBus(i)->matrixNumValues();
00651 p_network->getBus(i)->matrixGetValues(values,rows,cols);
00652 for (j=0; j<nvals; j++) {
00653 if (flag) {
00654 matrix.addElement(rows[j],cols[j],values[j]);
00655 } else {
00656 matrix.setElement(rows[j],cols[j],values[j]);
00657 }
00658 }
00659 }
00660 }
00661 delete [] values;
00662 delete [] rows;
00663 delete [] cols;
00664 }
00665
00666
00667
00668
00669
00670
00671 void loadBranchData(gridpack::math::Matrix &matrix, bool flag)
00672 {
00673 int i, j, nvals;
00674 ComplexType *values = new ComplexType[p_maxValues];
00675 int *rows = new int[p_maxValues];
00676 int *cols = new int[p_maxValues];
00677 for (i=0; i<p_nBranches; i++) {
00678 nvals = p_network->getBranch(i)->matrixNumValues();
00679 if (nvals > 0) {
00680 int ncols = p_network->getBranch(i)->matrixNumCols();
00681 int rmin, rmax;
00682 bool isActive = p_network->getActiveBranch(i);
00683 if (ncols > 0) {
00684 rmin = p_network->getBranch(i)->matrixGetRowIndex(0);
00685 rmax = p_network->getBranch(i)->matrixGetRowIndex(ncols-1);
00686 }
00687 p_network->getBranch(i)->matrixGetValues(values,rows,cols);
00688 bool addElem;
00689 for (j=0; j<nvals; j++) {
00690 if (rows[j] >= p_minRowIndex && rows[j] <= p_maxRowIndex) {
00691 addElem = false;
00692 if (ncols > 0) {
00693 if (cols[j] >= rmin && cols[j] <= rmax) {
00694 if (isActive) addElem = true;
00695 } else {
00696 addElem = true;
00697 }
00698 } else {
00699 addElem = true;
00700 }
00701 if (addElem) {
00702 if (flag) {
00703 matrix.addElement(rows[j],cols[j],values[j]);
00704 } else {
00705 matrix.setElement(rows[j],cols[j],values[j]);
00706 }
00707 }
00708 }
00709 }
00710 }
00711 }
00712 delete [] values;
00713 delete [] rows;
00714 delete [] cols;
00715 }
00716
00717
00718 int p_me;
00719 int p_nNodes;
00720
00721
00722 boost::shared_ptr<_network> p_network;
00723 int p_nBuses;
00724 int p_nBranches;
00725
00726
00727 int p_iDim;
00728 int p_jDim;
00729 int p_minRowIndex;
00730 int p_maxRowIndex;
00731 int p_maxValues;
00732 int p_colBlockSize;
00733 #ifdef NZ_PER_ROW
00734 int* p_nz_per_row;
00735 #endif
00736
00737 int* p_row_Offsets;
00738 int* p_col_Offsets;
00739
00740
00741 int g_bus_row_offsets;
00742 int g_bus_column_offsets;
00743 int g_branch_row_offsets;
00744 int g_branch_column_offsets;
00745 int p_GAgrp;
00746
00747
00748 gridpack::utility::CoarseTimer *p_timer;
00749
00750 };
00751
00752 }
00753 }
00754
00755 #endif //GENMATRIXMAP_HPP_